package com.csu.ecbackend.controller;


import com.csu.ecbackend.annotation.Record;
import com.csu.ecbackend.bean.User;
import com.csu.ecbackend.commom.ActionCode;
import com.csu.ecbackend.commom.CommonResponse;
import com.csu.ecbackend.service.KeywordService;
import com.csu.ecbackend.vo.FeedbackVO;
import com.csu.ecbackend.vo.KeywordVO;
import com.csu.ecbackend.vo.RecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@CrossOrigin
@RequestMapping("/keyword")
@Api(value = "关键词业务有关接口", tags = "关键词")
public class KeywordController
{

      @Autowired
      KeywordService keywordService;

      @GetMapping("/compete/{keyword}")
      @ResponseBody
      @ApiOperation("获得某关键词的竞争关键词, sort为排序方式：0为按竞争度，1为按点赞数，2为按点踩数")
      @Record(action = ActionCode.QUERY)
      public CommonResponse<KeywordVO> getCompetes(@PathVariable String keyword, @RequestParam int sort, HttpSession session)
      {
            User user = (User) session.getAttribute("login_user");
            return keywordService.getCompetes(keyword, sort, user);
      }

      @GetMapping("/compete/{keyword}/{minCompete}/{maxCompete}")
      @ResponseBody
      @ApiOperation("获得某关键词的竞争关键词，竞争度在mincompete和maxcompete之间, sort为排序方式：0为按竞争度，1为按点赞数，2为按点踩数")
      @Record(action = ActionCode.QUERY)
      public CommonResponse<KeywordVO> getCompetes(@PathVariable String keyword, @RequestParam int sort, @PathVariable double minCompete, @PathVariable double maxCompete, HttpSession session)
      {
            User user = (User) session.getAttribute("login_user");
            if (minCompete > maxCompete)
            {
                  double temp = minCompete;
                  minCompete = maxCompete;
                  maxCompete = temp;
            }
/*            User login_user = (User) session.getAttribute("login_user");
            String ipAddress = UtilKit.getIp(request);
            if (login_user != null) {
                  keywordService.insertRecord(login_user.getUserid(), keyword, ipAddress);
            } else
                  keywordService.insertRecord(0, keyword, ipAddress);*/
            return keywordService.getCompetes(keyword, sort, minCompete, maxCompete, user);
      }

      @GetMapping("/compete/{keyword}/{preference}")
      @ResponseBody
      @ApiOperation("根据偏好获得某关键词的竞争关键词, sort为排序方式：0为按竞争度，1为按点赞数，2为按点踩数")
      @Record(action = ActionCode.QUERY)
      public CommonResponse<KeywordVO> getCompetes(@PathVariable String keyword, @RequestParam int sort, HttpSession session, @PathVariable String preference)
      {
            User user = (User) session.getAttribute("login_user");
            return keywordService.getCompetes(keyword, sort, user, preference);
      }

      @GetMapping("/compete/{keyword}/{minCompete}/{maxCompete}/{preference}")
      @ResponseBody
      @ApiOperation("根据偏好获得某关键词的竞争关键词，竞争度在mincompete和maxcompete之间, sort为排序方式：0为按竞争度，1为按点赞数，2为按点踩数")
      @Record(action = ActionCode.QUERY)
      public CommonResponse<KeywordVO> getCompetes(@PathVariable String keyword, @RequestParam int sort, @PathVariable double minCompete, @PathVariable double maxCompete, HttpSession session, @PathVariable String preference)
      {
            User user = (User) session.getAttribute("login_user");
            if (minCompete > maxCompete)
            {
                  double temp = minCompete;
                  minCompete = maxCompete;
                  maxCompete = temp;
            }
/*            User login_user = (User) session.getAttribute("login_user");
            String ipAddress = UtilKit.getIp(request);
            if (login_user != null) {
                  keywordService.insertRecord(login_user.getUserid(), keyword, ipAddress);
            } else
                  keywordService.insertRecord(0, keyword, ipAddress);*/
            return keywordService.getCompetes(keyword, sort, minCompete, maxCompete, user, preference);
      }

      @GetMapping("/record/{keyword}")
      @ResponseBody
      @ApiOperation("获得某关键词的近七天的数据统计")
      public CommonResponse<RecordVO> getRecordWeek(@PathVariable String keyword)
      {
            return keywordService.getRecordWeek(keyword);
      }

      @GetMapping("/record/{keyword}/{day}")
      @ResponseBody
      @ApiOperation("获得某关键词的近day天的数据统计")
      public CommonResponse<RecordVO> getRecordWeek(@PathVariable String keyword, @PathVariable int day)
      {
            return keywordService.getRecord(keyword, day);
      }

      @GetMapping("/feedback/{keyword}/{compete}")
      @ResponseBody
      @ApiOperation("获得某关键词和竞争词的七天点赞数据")
      public CommonResponse<FeedbackVO> getFeedbackWeek(@PathVariable String keyword, @PathVariable String compete)
      {
            return keywordService.getFeedBackWeek(keyword, compete);
      }

      @GetMapping("/feedback/{keyword}/{compete}/{day}")
      @ResponseBody
      @ApiOperation("获得某关键词和竞争词的day天点赞数据")
      public CommonResponse<FeedbackVO> getFeedbackWeek(@PathVariable String keyword, @PathVariable String compete, @PathVariable int day)
      {
            return keywordService.getFeedBack(keyword, compete, day);
      }

}
